package com.softcits.hw.aop;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Arrays;

public class LoggerHandler implements InvocationHandler {
    
    private Object target;
    
    public LoggerHandler(Object target) {
        this.target = target;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("Method Start: " + Arrays.toString(args));
        Object obj = method.invoke(this.target, args);
        
        System.out.println("Method End: " + obj);
        return obj;
    }
    
    
    public static Object createLoggerProxy(Object target) {
        Object proxyObj = null;
        InvocationHandler ih = new LoggerHandler(target);
        proxyObj = Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), ih);
        
        return proxyObj;
    }

}
